<?php

namespace App\Http\Service;

use App\Model\Bill;
use App\Model\BusinessConf;
use Carbon\Carbon;

class ContractService
{
    public function createBill($contract)
    {
        // 生成账单
        $businessConf = BusinessConf::query()->where('business_id', $contract->business_id)->first();
        if ($contract->contractRent->deposit > 0) {
            $bill = new Bill();
            $bill->contract_id = $contract->id;
            $bill->amount = $contract->contractRent->deposit;
            $bill->business_id = $contract->business_id;
            if ($businessConf && $businessConf->fee_payer == 2) {
                $bill->fee_payer = 2;
            }else{
                $bill->fee_payer = 1;
            }
            $bill->house_id = $contract->house_id;
            $bill->mobile = $contract->mobile;
            $bill->title = "押金";
            $bill->type = 1;
            $start_date = Carbon::createFromFormat('Y-m-d', $contract->contractRent->start_at);
            $end_date = $contract->expired_at;
            $bill->start_date = $start_date->toDateString();
            $bill->end_date = $end_date;
            $bill->pay_date = $contract->contractRent->start_at;
            if ($contract->contractRent->is_deposit_paid) {
                $bill->is_paid = 1;
                $bill->paid_at = now()->toDateTimeString();
            }
            $bill->target_id='u_'.time().rand(0,999);
            $bill->save();
        }
        for ($i = 1; $i <= (ceil($contract->contractRent->month_num / $contract->contractRent->rent_num)); $i ++) {
            $bill = new Bill();
            $bill->contract_id = $contract->id;
            $bill->amount = $contract->contractRent->amount * $contract->contractRent->rent_num;
            $bill->business_id = $contract->business_id;
            $bill->house_id = $contract->house_id;
            $bill->mobile = $contract->mobile;
            $bill->title = "第{$i}期 - 房租";
            $bill->stage = $i;
            $bill->type = 1;
            $bill->target_id='u_'.time().rand(0,999).$i;
            $start_date = Carbon::createFromFormat('Y-m-d', $contract->contractRent->start_at);
            if ($i > 1) {
                $start_date->addMonths(($i - 1) * $contract->contractRent->rent_num);
            }
            $end_date = (clone $start_date)->addMonths($contract->contractRent->rent_num);
            $bill->start_date = $start_date->toDateString();
            $bill->end_date = $end_date->toDateString();
            if ($contract->contractRent->pay_type == 1) {
                $bill->pay_date = $start_date->subDays($contract->contractRent->days)->toDateString();
            } else {
                $bill->pay_date = $start_date->firstOfMonth()->addDays($contract->contractRent->pay_day)->toDateString();
            }
            if ($bill->pay_date < now()->toDateString()) {
                $bill->pay_date = now()->toDateString();
            }
            if ($contract->contractRent->paid_stage >= $i) {
                $bill->is_paid = 1;
                $bill->paid_at = now()->toDateTimeString();
            }
            if ($businessConf && $businessConf->fee_payer == 2) {
                $bill->fee_payer = 2;
            }else{
                $bill->fee_payer = 1;
            }
            $bill->save();
        }
    }
}
